//
//  FunctionCell.m
//  diaoyuren
//
//  Created by 钓鱼人 on 15/12/18.
//  Copyright © 2015年 game. All rights reserved.
//

#import "FunctionCell.h"
#import "CollectionViewCell.h"
#import "MenuModel.h"
#import "UIImageView+WebCache.h"

@interface FunctionCell ( )<UICollectionViewDataSource,UICollectionViewDelegate>
{

    MenuModel  *_menuModel;

}
@end

@implementation FunctionCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        
        [self CreatColletion];
    }

    return self;
}


-(void)setIndexModel:(IndexModel *)indexModel{
    _indexModel = indexModel;
    self.allAry = [NSMutableArray arrayWithArray:_indexModel.menu];

    
    if (_indexModel.menu.count!=0) {
        
        static dispatch_once_t oneceToken;
        
        dispatch_once(&oneceToken, ^{
         
            [self.collectionview reloadData];
        });
 
    }
}

-(void)CreatColletion{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    CGFloat yd; //竖间隙；
    CGFloat h;//collection高度
    if (kDIEIsIPhone4SOrLess) {
        yd = 15;
        h = SCREEN_HEIGHT*0.32;
        layout.minimumLineSpacing = yd+5;  //最小竖间距
        self.CellHight = h+10;
        
    }else{
        yd = 20;
        h = SCREEN_HEIGHT*0.3;
        layout.minimumLineSpacing =10;
        self.CellHight = h-10;
    }

    CGFloat xd = 15;//横间隙
    
    layout.itemSize = CGSizeMake((SCREEN_WIDTH-xd*8)/5,(h-10*3)/2);
    layout.sectionInset = UIEdgeInsetsMake(10, 20, 10, 20);
    
    //最小行间距
    layout.minimumInteritemSpacing = xd*2;
    
     self.collectionview = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,h) collectionViewLayout:layout];
    self.collectionview.scrollEnabled = NO;
    self.collectionview.delegate=self;
    self.collectionview.dataSource=self;
    [self.collectionview registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    [self.contentView addSubview:self.collectionview];
}

//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (_allAry.count!=0) {
        self.collectionview.backgroundColor= [UIColor whiteColor];
    }else{
    
    self.collectionview.backgroundColor= tabGropBackColor;
    }
    
    return _allAry.count;
}

//collectionViewcell显示
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell"  forIndexPath:indexPath];
    _menuModel = _allAry[indexPath.item];
    
    NSURL *imageUrl;
    if (kDIEIsIPhone6P) {
        imageUrl = [NSURL URLWithString:_menuModel.icon_3x];
    }else{
        imageUrl = [NSURL URLWithString:_menuModel.icon_2x];
    }
    [cell.image sd_setImageWithURL:imageUrl];
    cell.title.text = _menuModel.name;
    
    return cell;
    
}
//collectionView点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    [self.deleget FunctionCellSelect:indexPath.item];
   
    
}
@end
